package com.huishu.goods.controller;

import com.alibaba.excel.EasyExcel;
import com.huishu.commons.pojo.Result;
import com.huishu.goods.execl.*;
import com.huishu.goods.service.GiveService;
import com.huishu.goods.service.PortfolioProductsService;
import com.huishu.goods.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName：ImportGoodsController
 * @Author: ljj
 * @Date: 2023/4/8 10:48
 */
@RestController
@RequestMapping("/import")
@Slf4j
public class ImportGoodsController {

    @Autowired
    private ProductService productService;

    @Autowired
    private PortfolioProductsService portfolioProductsService;

    @Autowired
    private GiveService giveService;


    @GetMapping("/import-goods")
    public Result importGoods(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ProductExecl.class, new ProductDataListener(productService)).sheet().doRead();
        return Result.success();
    }

    @Transactional
    @GetMapping("/import-portfolio-goods")
    public Result importPortfolioGoods(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),
                PorfolioProductExecl.class,
                new PorfolioProductDataListener(productService,portfolioProductsService)).sheet().doRead();
        return Result.success();
    }

    @GetMapping("/import-give-goods")
    public Result importGiveGoods(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ProductExecl.class, new ProductGiveDataListener(giveService)).sheet().doRead();
        return Result.success();
    }
}
